package com.xxl.job.executor.my.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <h3>xxl-job</h3>
 * <p>日期处理类</p>
 *
 * @author : 来朋
 * @date : 2020-12-28 17:47
 **/
public class MyDateUtil {

    public static Date addAndSubtractDaysByGetTime(Date dateTime/*待处理的日期*/, int n/*加减天数*/){
        //日期格式
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(df.format(new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L)));
        //注意这里一定要转换成Long类型，要不n超过25时会出现范围溢出，从而得不到想要的日期值
        return new Date(dateTime.getTime() + n * 24 * 60 * 60 * 1000L);
    }

    /**
     * 获取当天日期的前一天23:59:59
     * @param dateTime
     * @return
     */
    public static Date getLastDay(Date dateTime){
        Date lastDay=new Date(dateTime.getTime() + (-1) * 24 * 60 * 60 * 1000L);
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(lastDay);
        cal1.set(Calendar.HOUR_OF_DAY, 23);
        cal1.set(Calendar.MINUTE, 59);
        cal1.set(Calendar.SECOND, 59);
        cal1.set(Calendar.MILLISECOND, 99);
        return cal1.getTime();
    }

    /**
     * 获取当前日期的00:00:00
     * @param dateTime
     * @return
     */
    public static Date getNowDay(Date dateTime){
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(dateTime);
        cal1.set(Calendar.HOUR_OF_DAY, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        return cal1.getTime();
    }


}
